
var server_options={auto_reconnect:true};
var db_options={
        w:-1,// 设置w=-1是mongodb 1.2后的强制要求，见官方api文档
        safe:true,
        logger:{
        doDebug:true,
        debug:function(msg,obj){
            console.log('[debug]',msg);
        },
        log:function(msg,obj){
            console.log('[log]',msg);
        },
        error:function(msg,obj){
            console.log('[error]',msg);
        }
    }
};


var dbName = "bett";
var	mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, server_options);
var db = new mongodb.Db(dbName, server, db_options);

exports.save2Db = function(collectionName, obj, callback){
	//var server = new mongodb.Server('localhost', 27017, {auto_reconnect:true});
	//var db = new mongodb.Db(dbName, server, {safe:true});
	db.open(function(err, db){
		if(!err){
			console.log('success connect!');
			db.collection(collectionName, {safe:true}, function(err, collection){
				if(err){
					console.log(err);
					return;
				}
				//var tmp1 = {title:'hello',number:1};
				collection.insert(obj, {safe:true}, function(err,result){
					//-----------opt tmp1
					console.log(result);
					callback(result);
				}); 
			});
		}else{
			console.log('fail connect!');
		}
		//db.on('close', test.done.bind(test));
		//if(null != db)
		//	db.close();
	});
};

exports.update2Db = function(collectionName, condition, callback){
	
	db.open(function(err, db){
		if(!err){
			console.log('success connect!');
			db.collection(collectionName, {safe:true}, function(err, collection){
				if(err){
					console.log(err);
					return;
				}
				//var tmp1 = {title:'hello',number:1};
				collection.update(condition,{safe:true},function(err,result){
					callback(result);
				});

				
				/*collection.find().toArray(function(err,docs){
					//------------opt docs
					console.log('find');
					console.log(docs);
				}); */
			});
		}else{
			console.log('fail connect!');
		}
		//db.on('close', test.done.bind(test));
		if(null != db)
			db.close();

	});
};

exports.removeFromDb = function(collectionName, condition, callback){
	var server = new mongodb.Server('localhost', 27017, {auto_reconnect:true});
	var db = new mongodb.Db(dbName, server, {safe:true});
	db.open(function(err, db){
		if(!err){
			console.log('success connect!');
			db.collection(collectionName, {safe:true}, function(err, collection){
				if(err){
					console.log(err);
					return;
				}
				//var tmp1 = {title:'hello',number:1};
				collection.remove(condition,{safe:true},function(err,result){
					callback(result);
				});


				
				/*collection.find().toArray(function(err,docs){
					//------------opt docs
					console.log('find');
					console.log(docs);
				}); */
			});
		}else{
			console.log('fail connect!');
		}
		if(null != db)
			db.close();
	});
};

exports.getFromDb = function(collectionName, condition, callback){
	var server = new mongodb.Server('localhost', 27017, {auto_reconnect:true});
	var db = new mongodb.Db(dbName, server, {safe:true});
	db.open(function(err, db){
		if(!err){
			console.log('success connect!');
			db.collection(collectionName, {safe:true}, function(err, collection){
				if(err){
					console.log(err);
					return;
				}
				//var tmp1 = {title:'hello',number:1};
				/*collection.insert(tmp1, {safe:true}, function(err,result){
					//-----------opt tmp1
					console.log(result);
				}); */
				
				collection.find(condition).toArray(function(err,docs){
					//------------opt docs
					console.log('find');
					console.log(docs);
					//----------start callback
					callback(docs);
				}); 


			});
		}else{
			console.log('fail connect!');
		}
		if(null != db)
			db.close();
	});
};
